package com.lemon.rule.express.instruction.op;

import com.lemon.rule.express.Operator;

public class OperatorMinMax extends Operator {
    public OperatorMinMax(String name) {
        this.name = name;
    }

    public Object executeInner(Object[] list) throws Exception {
        if (list.length == 0) {
            throw new Exception("�������쳣");
        }
        Object result = list[0];

        for (int i = 1; i < list.length; i++)
            result = executeInner(result, list[i]);
        return result;
    }

    public Object executeInner(Object op1, Object op2) throws Exception {
        Object result = null;
        int compareResult = Operator.compareData(op1, op2);
        if (this.name.equals("min")) {
            if (compareResult < 0)
                result = op1;
            else
                result = op2;
        } else if (this.name.equals("max")) {
            if (compareResult < 0)
                result = op2;
            else
                result = op1;
        }
        return result;
    }
}